package org.schat.sockets;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

import com.google.gson.Gson;

public class MessageEncoder implements Encoder.Text<ChatMessage> {

	Gson gson;

	@Override
	public void destroy() {
		gson = null;
	}

	@Override
	public void init(EndpointConfig config) {
		gson = new Gson();
	}

	@Override
	public String encode(ChatMessage msg) throws EncodeException {
		if (msg == null) {
			//todo add some error message or something
			msg = new ChatMessage();
		}
		return gson.toJson(msg);
	}

}
